Stroke = Stroke or Class("Stroke")

Console.log("Stroke")
StrokePoint = StrokePoint or Class("StrokePoint")
function StrokePoint:ctor(x, y)
    self.x = x
    self.y = y
end
function Stroke:ctor(borderPixels)
    self._borderPixes = borderPixels
    self.space = 8
end

function Stroke:generatePoints()
    local sx, sy
    self._borderPixes:map(function (x, y, v)
        if v then
            sx, sy = x, y
            return true
        end
    end)
    if sx and sy then
        self.start = StrokePoint.new(sx, sy)
        local points = {}
        for i = -self.space, self.space do
            for j = -self.space, self.space do
                local x, y = sx + i, sy + j
                if self._borderPixes:get(x, y) then
                    if i == -self.space or i == self.space or
                        j == -self.space or j == self.space then
                        table.insert(points, {x, y})
                    end
                    self._borderPixes:set(x, y, false)
                end
            end
        end

        local curPoint = self.start
        while #points > 0 do
            curPoint.nextPoint = StrokePoint.new(points[1][1], points[1][2])
            curPoint = curPoint.nextPoint
            local sx, sy = curPoint.x, curPoint.y
            points = {}
            for i = -self.space, self.space do
                for j = -self.space, self.space do
                    local x, y = sx + i, sy + j
                    if self._borderPixes:get(x, y) then
                        self._borderPixes:set(x, y, false)
                        if i == -self.space or i == self.space or
                            j == -self.space or j == self.space then
                            table.insert(points, {x, y})
                        end
                    end
                end
            end
        end
        curPoint.nextPoint = self.start
    end
end